package com.cy.aircompressor.main.home.lifecycle.provider;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;

import com.cy.aircompressor.databinding.ItemPartsManagerLayoutBinding;
import com.cy.aircompressor.entity.component.Component;
import com.cy.aircompressor.main.home.lifecycle.detail.AirCompressorDetailDialog;
import com.cy.aircompressor.main.home.lifecycle.detail.PartsDetailDialog;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.BaseBindViewHolder;

import java.util.concurrent.TimeUnit;

public class PartsManagerProvider extends ItemViewBinder<Component, PartsManagerProvider.ViewHolder> {
    Context mContext;
    FragmentManager fragmentManager;

    public PartsManagerProvider(Context mContext, FragmentManager fragmentManager) {
        this.mContext = mContext;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    protected PartsManagerProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemPartsManagerLayoutBinding.inflate(inflater, parent, false));

    }

    @Override
    protected void onBindViewHolder(@NonNull PartsManagerProvider.ViewHolder holder, @NonNull Component entity) {
        holder.mBinding.serial.setText(String.valueOf(holder.getAdapterPosition()));
        holder.mBinding.name.setText(entity.getName());
        holder.mBinding.brand.setText(entity.getBrand());
        holder.mBinding.model.setText(entity.getModel());
        holder.mBinding.installDate.setText(entity.getInstallDate());
        holder.mBinding.installLocation.setText(entity.getCompressorPartDesc());
        holder.mBinding.sn.setText(entity.getSn());
        holder.mBinding.manufacturer.setText(entity.getManufacturer());
        holder.mBinding.powerStationName.setText(entity.getPowerStationName());
        holder.mBinding.airName.setText(entity.getCompressorName());
        holder.mBinding.status.setText(entity.getStatusDesc());
        holder.itemView.setBackgroundColor(Color.parseColor(holder.getAdapterPosition() % 2 == 0 ? "#1B485E" : "#0E3A50"));

        RxView.clicks(holder.mBinding.detail)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    PartsDetailDialog.showDialogFragment(fragmentManager, "detail", entity);
                });
    }


    public class ViewHolder extends BaseBindViewHolder<ItemPartsManagerLayoutBinding> {

        protected ViewHolder(ItemPartsManagerLayoutBinding itemView) {
            super(itemView);
        }
    }


}
